package com.google.android.exoplayer2.extractor.amr;

import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.Extractor;
import com.huawei.hms.support.api.entity.core.JosStatusCodes;
import java.io.EOFException;
import java.io.IOException;
import java.util.Arrays;
import o.adq;
import o.adv;
import o.adw;
import o.ady;
import o.adz;
import o.aea;
import o.aea.e;
import o.aef;
import o.aei;
import o.anr;

public final class AmrExtractor implements Extractor {
    private static final int ʻ = ˋ[8];
    public static final adw ˊ = aei.ˏ;
    private static final int[] ˋ = new int[]{18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1};
    private static final int[] ˎ = new int[]{13, 14, 16, 18, 20, 21, 27, 32, 6, 7, 6, 6, 1, 1, 1, 1};
    private static final byte[] ˏ = anr.ˊ("#!AMR\n");
    private static final byte[] ॱ = anr.ˊ("#!AMR-WB\n");
    private ady ʻॱ;
    private long ʼ;
    private boolean ʽ;
    private boolean ˈ;
    private int ˊॱ;
    private int ˋॱ;
    private boolean ˏॱ;
    private long ͺ;
    private int ॱˊ;
    private aef ॱˋ;
    @Nullable
    private aea ॱˎ;
    private final int ॱॱ;
    private long ॱᐝ;
    private final byte[] ᐝ;
    private int ᐝॱ;

    public AmrExtractor() {
        this(0);
    }

    public AmrExtractor(int i) {
        this.ॱॱ = i;
        this.ᐝ = new byte[1];
        this.ॱˊ = -1;
    }

    public boolean ˎ(adv o_adv) throws IOException, InterruptedException {
        return ˊ(o_adv);
    }

    public void ˏ(ady o_ady) {
        this.ʻॱ = o_ady;
        this.ॱˋ = o_ady.ˊ(0, 1);
        o_ady.ˊ();
    }

    public int ˎ(adv o_adv, adz o_adz) throws IOException, InterruptedException {
        if (o_adv.ˊ() != 0 || ˊ(o_adv)) {
            ˋ();
            int ˋ = ˋ(o_adv);
            ˏ(o_adv.ॱ(), ˋ);
            return ˋ;
        }
        throw new ParserException("Could not find AMR header.");
    }

    public void ˊ(long j, long j2) {
        this.ʼ = 0;
        this.ˊॱ = 0;
        this.ˋॱ = 0;
        if (j == 0 || !(this.ॱˎ instanceof adq)) {
            this.ॱᐝ = 0;
        } else {
            this.ॱᐝ = ((adq) this.ॱˎ).ˊ(j);
        }
    }

    public void ॱ() {
    }

    private boolean ˊ(adv o_adv) throws IOException, InterruptedException {
        if (ˎ(o_adv, ˏ)) {
            this.ʽ = false;
            o_adv.ॱ(ˏ.length);
            return true;
        } else if (!ˎ(o_adv, ॱ)) {
            return false;
        } else {
            this.ʽ = true;
            o_adv.ॱ(ॱ.length);
            return true;
        }
    }

    private boolean ˎ(adv o_adv, byte[] bArr) throws IOException, InterruptedException {
        o_adv.ˏ();
        byte[] bArr2 = new byte[bArr.length];
        o_adv.ˎ(bArr2, 0, bArr.length);
        return Arrays.equals(bArr2, bArr);
    }

    private void ˋ() {
        if (!this.ˈ) {
            this.ˈ = true;
            this.ॱˋ.ˎ(Format.ˋ(null, this.ʽ ? "audio/amr-wb" : "audio/3gpp", null, -1, ʻ, 1, this.ʽ ? 16000 : JosStatusCodes.RTN_CODE_COMMON_ERROR, -1, null, null, 0, null));
        }
    }

    private int ˋ(adv o_adv) throws IOException, InterruptedException {
        if (this.ˋॱ == 0) {
            try {
                this.ˊॱ = ॱ(o_adv);
                this.ˋॱ = this.ˊॱ;
                if (this.ॱˊ == -1) {
                    this.ͺ = o_adv.ˊ();
                    this.ॱˊ = this.ˊॱ;
                }
                if (this.ॱˊ == this.ˊॱ) {
                    this.ᐝॱ++;
                }
            } catch (EOFException e) {
                return -1;
            }
        }
        int ˏ = this.ॱˋ.ˏ(o_adv, this.ˋॱ, true);
        if (ˏ == -1) {
            return -1;
        }
        this.ˋॱ -= ˏ;
        if (this.ˋॱ > 0) {
            return 0;
        }
        this.ॱˋ.ˋ(this.ॱᐝ + this.ʼ, 1, this.ˊॱ, 0, null);
        this.ʼ += 20000;
        return 0;
    }

    private int ॱ(adv o_adv) throws IOException, InterruptedException {
        o_adv.ˏ();
        o_adv.ˎ(this.ᐝ, 0, 1);
        byte b = this.ᐝ[0];
        if ((b & 131) <= 0) {
            return ˊ((b >> 3) & 15);
        }
        throw new ParserException("Invalid padding bits for frame header " + b);
    }

    private int ˊ(int i) throws ParserException {
        if (ॱ(i)) {
            return this.ʽ ? ˋ[i] : ˎ[i];
        } else {
            throw new ParserException("Illegal AMR " + (this.ʽ ? "WB" : "NB") + " frame type " + i);
        }
    }

    private boolean ॱ(int i) {
        return i >= 0 && i <= 15 && (ˏ(i) || ˋ(i));
    }

    private boolean ˏ(int i) {
        return this.ʽ && (i < 10 || i > 13);
    }

    private boolean ˋ(int i) {
        return !this.ʽ && (i < 12 || i > 14);
    }

    private void ˏ(long j, int i) {
        if (!this.ˏॱ) {
            if ((this.ॱॱ & 1) == 0 || j == -1 || !(this.ॱˊ == -1 || this.ॱˊ == this.ˊॱ)) {
                this.ॱˎ = new e(-9223372036854775807L);
                this.ʻॱ.ˎ(this.ॱˎ);
                this.ˏॱ = true;
            } else if (this.ᐝॱ >= 20 || i == -1) {
                this.ॱˎ = ˏ(j);
                this.ʻॱ.ˎ(this.ॱˎ);
                this.ˏॱ = true;
            }
        }
    }

    private aea ˏ(long j) {
        return new adq(j, this.ͺ, ˎ(this.ॱˊ, 20000), this.ॱˊ);
    }

    private static int ˎ(int i, long j) {
        return (int) ((((long) (i * 8)) * 1000000) / j);
    }
}
